<template>
  <contentModel
    v-if="modelValue"
    :title="selectId ? '编辑' : '新增'"
    @open="open"
    @close="cancel"
  >
    <template #footer>
      <a-space>
        <a-button type="outline" @click="handleCancel(record, rowIndex)"
          >取消</a-button
        >
        <a-button
          type="primary"
          :loading="loading"
          @click="handleSubmit(record, rowIndex)"
          >保存</a-button
        >
      </a-space>
    </template>
    <a-form
      ref="formRef"
      layout="vertical"
      :model="form"
      auto-label-width
      :rules="rules"
    >
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="secretLevel" label="密级">
            <a-input v-model="selectBatchRow.secretLevelName" readonly />
            <!-- <secretLevelSelect v-model="form.secretLevel" style="width: 100%" /> -->
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="mdsItemCode" label="物料编码">
            <a-input v-model="selectBatchRow.mdsItemCode" readonly />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="mdsItemName" label="物料名称">
            <a-input v-model="selectBatchRow.mdsItemName" readonly />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="partQty" label="子件结构对数">
            <a-input-number v-model="form.partQty" placeholder="请输入" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="reqQty" label="应到数量">
            <a-input-number v-model="form.reqQty" placeholder="请输入" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="mdsInventoryId" label="存放区域">
            <a-select v-model="form.mdsInventoryId" placeholder="请选择">
              <a-option>成品库房</a-option>
            </a-select>
            <a-select v-model="form.mdsLocatorId" placeholder="请选择">
              <a-option>01</a-option>
              <a-option>01-07</a-option>
              <a-option>01-08</a-option>
              <a-option>01-09</a-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="mdsUnitRate" label="转换系数">
            <a-input
              v-model="selectBatchRow.mdsUnitRate"
              placeholder="请输入"
              readonly
            />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="invReceiveQty" label="到货确认数量(库存)(件)">
            <a-input-number v-model="form.invReceiveQty" placeholder="请输入" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="pmsReceiveQty" label="到货确认数量(采购)(件)">
            <a-input-number v-model="form.pmsReceiveQty" placeholder="请输入" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="batchNo" label="批号">
            <a-input v-model="form.batchNo" placeholder="请输入" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="itemSerialNo" label="件号">
            <a-input v-model="form.itemSerialNo" placeholder="请输入" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="measurementLength" label="长/宽(M)">
            <a-row :gutter="16" style="width: 100%">
              <a-col :span="12">
                <a-input-number
                  v-model="form.measurementLength"
                  placeholder="请输入长"
                />
              </a-col>
              <a-col :span="12">
                <a-input-number
                  v-model="form.measurementWidth"
                  placeholder="请输入宽"
                />
              </a-col>
            </a-row>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="parentItemSerialNo" label="父型号件号">
            <a-input v-model="form.parentItemSerialNo" placeholder="请输入" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="manufacturer" label="制造商">
            <a-input v-model="form.manufacturer" placeholder="请输入" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="manuDate" label="制造日期">
            <a-date-picker
              v-model="form.manuDate"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              style="width: 100%"
              placeholder="请选择"
            />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="lfackoryDate" label="出厂日期">
            <a-date-picker
              v-model="form.lfackoryDate"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              style="width: 100%"
              placeholder="请选择"
            />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="arrivalDate" label="实际到货日期">
            <a-date-picker
              v-model="form.arrivalDate"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              style="width: 100%"
              placeholder="请选择"
            />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="qnitQty" label="装箱数量">
            <a-input-number
              v-model="form.qnitQty"
              :min="0"
              placeholder="请输入"
            />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="confineFlag" label="是否限用">
            <a-select v-model="form.confineFlag" placeholder="请选择">
              <a-option value="Y">是</a-option>
              <a-option value="N">否</a-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="confineProcuct" label="限用型号">
            <!-- <inputModal
              v-model="form.confineProcuct"
              :label-value="form.confineProcuct"
              :value-name="'confineProcuct'"
              :title="'限用型号'"
              :type="'supplyName'"
            /> -->
            <a-input v-model="form.confineProcuct" placeholder="请输入" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="confineBatch" label="限用批次">
            <a-input v-model="form.confineBatch" placeholder="请输入" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="confineLot" label="限用架次">
            <a-input v-model="form.confineLot" placeholder="请输入" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="confineDesc" label="限用说明">
            <a-input v-model="form.confineDesc" placeholder="请输入" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="recprtlotNo" label="供方合格证号">
            <a-input v-model="form.recprtlotNo" placeholder="请输入" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="proMdsItemId" label="机型">
            <inputModal
              v-model="form.proMdsItemId"
              :label-value="form.proMdsItemName"
              :value-name="'id'"
              :label-name="'materialCode'"
              :title="'机型'"
              :default-query="{
                materialType: '6',
                materialStatus: '1,3',
                secretLevel: selectBatchRow.secretLevel,
              }"
              :type="'materialCode'"
            />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="weight" label="重量">
            <a-input v-model="form.weight" placeholder="请输入" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="weightUnit" label="重量单位">
            <a-input v-model="form.weightUnit" placeholder="请输入" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="reviewCheckSpecs" label="入场复验规范">
            <a-input v-model="form.reviewCheckSpecs" placeholder="请输入" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="qaCl" label="质保条款">
            <a-input v-model="form.qaCl" placeholder="请输入" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="note" label="到货备注">
            <a-input v-model="form.note" placeholder="请输入" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="storageGrep" label="存放组别">
            <dict-select
              v-model="form.storageGrep"
              allow-clear
              dict-type="storageGroup"
              placeholder="请选择"
            ></dict-select>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="singleEquivalentWeight" label="单件当量g">
            <a-input-number
              v-model="form.singleEquivalentWeight"
              placeholder="请输入"
            />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="importantNotes" label="重要记事">
            <a-input v-model="form.importantNotes" placeholder="请输入" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="calcBasis" label="三期起始依据">
            <a-select v-model="formThree.calcBasis" placeholder="请选择">
              <a-option>制造日期</a-option>
              <a-option>出厂日期</a-option>
              <a-option>到货日期</a-option>
              <a-option>检验日期</a-option>
              <a-option>其他</a-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="threeStartDate" label="三期起始日期">
            <a-date-picker
              v-model="formThree.threeStartDate"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              style="width: 100%"
              placeholder="请选择"
            />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="oilSealDate" label="下次油封日期">
            <a-date-picker
              v-model="formThree.oilSealDate"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              style="width: 100%"
              placeholder="请选择"
            />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="recheckDate" label="下次复检期">
            <a-date-picker
              v-model="formThree.recheckDate"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              style="width: 100%"
              placeholder="请选择"
            />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="storageDate" label="有效期">
            <a-date-picker
              v-model="formThree.storageDate"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              style="width: 100%"
              placeholder="请选择"
            />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="invDateNum" label="库存期">
            <a-date-picker
              v-model="formThree.invDateNum"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              style="width: 100%"
              placeholder="请选择"
            />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="archiveDate" label="封存日期">
            <a-date-picker
              v-model="formThree.archiveDate"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              style="width: 100%"
              placeholder="请选择"
            />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="preservationTime" label="封存特性">
            <a-input
              v-model="formThree.preservationTime"
              placeholder="请输入"
            />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="acceptanceDate" label="验收日期">
            <a-date-picker
              v-model="formThree.acceptanceDate"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              style="width: 100%"
              placeholder="请选择"
            />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="lifetimeNum" label="总寿命">
            <a-input v-model="formThree.lifetimeNum" placeholder="请输入" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="scanfFirstDate" label="首翻期">
            <a-input v-model="formThree.scanfFirstDate" placeholder="请输入" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="scanfUseDate" label="使用期限">
            <a-input v-model="formThree.scanfUseDate" placeholder="请输入" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="scanfSureDate" label="保证期">
            <a-input v-model="formThree.scanfSureDate" placeholder="请输入" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="repairTime" label="返修日期">
            <a-date-picker
              v-model="formThree.repairTime"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              style="width: 100%"
              placeholder="请选择"
            />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="sealDate" label="油封期">
            <a-date-picker
              v-model="formThree.sealDate"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              style="width: 100%"
              placeholder="请选择"
            />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="materialSecretLevel" label="实物密级">
            <secretLevelSelect
              v-model="formThree.materialSecretLevel"
              :secret-level="selectBatchRow.secretLevel"
              style="width: 100%"
            />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="secretPeriod" label="密保期限">
            <a-input v-model="formThree.secretPeriod" placeholder="请输入" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <EditTable ref="editTable"></EditTable>
  </contentModel>
</template>

<script lang="ts" setup>
  import {
    defineAsyncComponent,
    inject,
    onMounted,
    reactive,
    ref,
    toRefs,
  } from 'vue';
  import dictSelect from '@/common/components/dict-select/index.vue';
  import {
    getPmsReceiveDetail,
    savePmsReceiveDetail,
    updatePmsReceiveDetail,
  } from '@/api/purchaseArrival/purchaseArrivalDetails';
  import { Message } from '@arco-design/web-vue';
  import inputModal from '@/common/components/search-modal/input-modal.vue';
  import secretLevelSelect from '@/common/components/cac-components/secret-level-select/index.vue';
  import { isEmptyObject } from '@/utils/is';
  import EditTable from './EditTable.vue';

  const contentModel = defineAsyncComponent(
    () => import('@/common/components/content-model/index.vue')
  );
  const props = defineProps({
    modelValue: {
      type: Boolean,
      default: false,
    },
    // eslint-disable-next-line vue/require-prop-types
    selectId: {
      default: null,
    },
  });
  const emits = defineEmits(['update:modelValue', 'fetchData']);
  const cancel = () => {
    emits('update:modelValue', false);
  };
  const selectBatchRow = inject('selectBatchRow');
  const { selectId } = toRefs(props);
  // const { cancel } = useModelFun();
  const formRef = ref(null);
  const editTable = ref(null);
  let form = reactive({});
  let formThree = reactive({});
  const loading = ref(false);
  const rules = {
    // secretLevel: [
    //   {
    //     required: true,
    //     message: '请选择',
    //   },
    // ],
    invReceiveQty: [
      {
        required: true,
        message: '请输入',
      },
    ],
    pmsReceiveQty: [
      {
        required: true,
        message: '请输入',
      },
    ],
    itemSerialNo: [
      {
        required: true,
        message: '请输入',
      },
    ],
    arrivalDate: [
      {
        required: true,
        message: '请选择',
      },
    ],
    value2: [
      // 机型
      {
        required: true,
        message: '请选择',
      },
    ],
  };
  const getDetailData = async () => {
    const { data } = await getPmsReceiveDetail(selectId?.value);
    formThree = Object.assign(formThree, data.receiveDetailBatchThreeVO);
    form = Object.assign(form, data.receiveDetailBatchVO);
    editTable.value.setData(data.receiveDetailBatchSoftwareVOUpdate);
    // Object.keys(data).forEach((key) => {
    //   form[key] = data[key];
    // });
  };
  const handleSubmit = async () => {
    const validate = await formRef.value.validate();
    if (validate) return;
    loading.value = true;
    try {
      // 先做验证后保存
      // await checkPmsReceiveLDetailBatch({
      //   pmsReceiveDetailId: form.pmsReceiveDetailId,
      //   pmsReceiveDetailBatchId: form.pmsReceiveDetailBatchId,
      //   invReceiveQty: form.invReceiveQty,
      //   outFactoryLossQty: form.outFactoryLossQty,
      //   inFactoryLossQty: form.inFactoryLossQty,
      //   itemSerialNo: form.itemSerialNo,
      //   mdsItemId: selectBatchRow.value.mdsItemId,
      // });
      const formData = {
        receiveDetailBatchVO: form,
        receiveDetailBatchThreeVO: formThree,
        receiveDetailBatchSoftwareVOInsert: editTable.value.getInsertData(),
        receiveDetailBatchSoftwareVOUpdate: editTable.value.getUpdateData(),
        receiveDetailBatchSoftwareVODelete: editTable.value.getDeleteData(),
      };
      if (!isEmptyObject(formData.receiveDetailBatchThreeVO)) {
        formData.receiveDetailBatchThreeVO.secretLevel =
          selectBatchRow.value.secretLevel;
      }
      if (form?.id) {
        await updatePmsReceiveDetail(formData);
        Message.success({
          content: '编辑成功！',
          duration: 5 * 1000,
        });
      } else {
        await savePmsReceiveDetail(formData);
        Message.success({
          content: '保存成功！',
          duration: 5 * 1000,
        });
        loading.value = false;
      }
      emits('fetchData');
      cancel();
    } catch (error) {
      console.log(error);
      loading.value = false;
    }
  };
  // const handleOk = () => {
  //   cancel();
  // };
  const handleCancel = () => {
    cancel();
  };
  onMounted(() => {
    if (selectId?.value) {
      getDetailData();
    } else {
      form.mdsItemCode = selectBatchRow.value.mdsItemCode;
      form.mdsItemName = selectBatchRow.value.mdsItemName;
      form.pmsReceiveId = selectBatchRow.value.pmsReceiveId;
      form.pmsReceiveDetailId = selectBatchRow.value.id;
      form.setupSource = 1; // 手动
      form.secretLevel = selectBatchRow.value.secretLevel;
      form.invReceiveQty = selectBatchRow.value.invReceiveQty;
      form.pmsReceiveQty = selectBatchRow.value.pmsReceiveQty;
      form.proMdsItemId = selectBatchRow.value.proMdsItemId;
      form.proMdsItemName = selectBatchRow.value.proMdsItemName;
    }
  });
  const open = () => {};
</script>
